// pages/schedule/scheduleList/scheduleList.js
import validate from '../../../utils/validate.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nums:['全部',1,2,3,4,5,6,7,8,9,10],
    num: '全部',
    storeid:'',
    storename:'',
    scheduleList:[],
    checkeds:[],
    CustomBar:app.globalData.CustomBar,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      storeid:options.storeid,
      storename:options.storename
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.search()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  search:function(){
    let that = this;
    let n = this.data.num
    if('全部' == n){
      n = 'all'
    }
    let data = {storeid:this.data.storeid,num:n}
    app.api.getSchedulelist(data).then(res => {
      if(res.code == 200){
        that.setData({
          scheduleList : res.rows,
          checkeds:[]
        })
      }
    })
  },
  selSchedule:function(){
    let schedule = this.data.scheduleList[e.currentTarget.dataset.index]

  },
  changeNum:function(e){
    this.setData({
      num: this.data.nums[e.detail.value]
    })
    this.search()
  },
  checkboxChange:function(e){
    this.setData({
      checkeds : e.detail.value
    })
    // let list = JSON.parse(JSON.stringify(this.data.scheduleList));
    // for(let index in list){
    //   list[index].checked = false
    //   for(let val of e.detail.value){
    //     if(index = val){
    //       list[index].checked = true;
    //       break;
    //     }
    //   }
    // }
  },
  submit:function(){
    let schedules = '';
    let gids = '';
    for(let val of this.data.checkeds){
      if(schedules != ''){
        schedules += ','
        gids += ','
      }
      let item =  this.data.scheduleList[val]
      schedules +=  item.scheduledate
      gids += item.gid
    }
    if( validate.validatenull(schedules)){
      app.dialog.waring('请选择至少一条档期');
      return;
    }
    let params = '?schedules=' + schedules
    params += '&gids=' + gids
    params += '&storeid=' + this.data.storeid
    params += '&storename=' + this.data.storename
    params += '&num=' + this.data.num
    wx.navigateTo({
      url: '../addschedule/addschedule'+params
    })
  }
})